/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.schedule.img;

import hope.platform.data.domain.HopeException;
import hope.platform.data.domain.SiteConnectionInfo;
import hope.platform.data.domain.SiteInfo;
import hope.platform.data.domain.entity.DataKindInfo;
import hope.platform.data.domain.type.SiteStatusType;
import hope.platform.data.server.data.header.HopeExceptionService;
import hope.platform.data.server.data.header.SiteConnectionInfoService;
import hope.platform.data.server.data.header.SiteInfoService;
import hope.platform.data.server.schedule.header.CheckSiteConnectionService;
import hope.platform.data.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/27 11:05
 */
@Service
@Slf4j
public class CheckSiteConnectionServiceImg implements CheckSiteConnectionService {

    @Autowired
    private SiteInfoService siteInfoService;

    @Autowired
    private SiteConnectionInfoService connectionService;

    @Autowired
    private HopeExceptionService hopeExceptionService;

    /**
     * 检查正常站点
     */
    @Override
    public void check() {
        //获取所有站点serialNumber
        List<SiteInfo> siteInfos = siteInfoService.getAllSiteInfo();
        for (SiteInfo siteInfo : siteInfos) {
            boolean flag = false;
            SiteConnectionInfo connection =
                    connectionService.getBySerialNumber(siteInfo.getSerialNumber());
            //计算出判断连接异常的最早时间。
            Date deadLine = TimeUtils.getBeforeTime(new Date(), 5);
            if(connection != null && connection.getDataKindInfoMap() != null) {
                for (Map.Entry<String, DataKindInfo> entry : connection.getDataKindInfoMap().entrySet()) {
                    //判断是否晚于最早时间
                    if ( entry.getValue().getTime().after(deadLine)) {
                        flag = true;
                        break;
                    }
                }
            }

            //判断为正常即运行中
            if (flag) {
                if (siteInfo.getSiteStatus().equals(SiteStatusType.EXCEPTION)) {
                    //从异常状态修改为正常状态
                    siteInfo.setSiteStatus(SiteStatusType.NORMAL);
                    try {
                        siteInfoService.updateSiteInfo(siteInfo);
                    } catch (Exception e) {
                        log.warn("site info update failed.", e);
                    }
                }
                continue;
            }
            if (!siteInfo.getSiteStatus().equals(SiteStatusType.NORMAL)) {
                //判定该站点已经有连接异常了
                setSiteInfoException(siteInfo);
            }

        }

    }

    private void setSiteInfoException(SiteInfo siteInfo) {
        //修改状态
        siteInfo.setSiteStatus(SiteStatusType.EXCEPTION);
        try {
            siteInfoService.updateSiteInfo(siteInfo);
        } catch (Exception e) {
            log.warn("update failed.", e);
        }
        //添加异常信息
        HopeException hopeException = new HopeException();
        hopeException.setSerialNumber(siteInfo.getSerialNumber());
        hopeException.setDataName(SiteStatusType.EXCEPTION);
        hopeException.setExceptionType(SiteStatusType.EXCEPTION);
        hopeException.setContent("5分钟内没有接到新的数据信息，请查看站点编号为"
                + siteInfo.getSerialNumber() + "是否有连接异常。");
        hopeExceptionService.addHopeException(hopeException);
    }
}
